/*
 * Powered By Rubyliu
 * QQ:876181
 * Since 2012 - 2013
 */

package com.henme.crm.service.product.impl;

import com.henme.crm.dao.product.ProductImgMapper;
import com.henme.crm.dao.product.ProductMapper;
import com.henme.crm.entity.product.Product;
import com.henme.crm.entity.product.ProductImg;
import com.henme.crm.service.product.ProductService;
import com.henme.crm.util.page.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Ruby liu
 * @version 1.0
 * @since 1.0
 */
@Service
public class ProductServiceImpl implements ProductService {
	
	protected static Logger logger =  LoggerFactory.getLogger("service");
	
	@Autowired
	private ProductMapper productMapper;


    @Autowired
    private ProductImgMapper productImgMapper;

	@Transactional

    public void insertProduct(Product  product,String fileArray){
		productMapper.insertOne(product);
        if(fileArray.length()>0){
            if(fileArray.startsWith(",")){
                fileArray=fileArray.substring(1);    //因为在前台传过来的是第一个字符是,开始的
            }
            String [] file=fileArray.split(",");
            ProductImg  productImg;
            for (String s : file) {
                productImg =new ProductImg();
                productImg.setImgsrc(s);
                productImg.setProductId(product.getId());
                productImgMapper.insertOne(productImg);
            }
        }
	}
	public void updateProduct(Product  product){
		productMapper.updateOne(product);
	}
	public void deleteProduct(long id){
		productMapper.deleteOne(id);
	}
	
	public Product selectProduct(long id){
	  return 	productMapper.selectOne(id);
	}
	
	public Page<Product> getProductList(Page<Product> page){
        List<Product> list = productMapper.findProductPage(page);
        page.setResult(list);
		return page;
	}
}